<#import "../main.html" as page>
    <@page.main current="/javaCode" title="JAVA代码生成器">

    <style type="text/css">
        .eleHide {
            display: none;
        }

        .divContent {
            box-shadow: 0 0 10px grey;
            padding: 20px;
        }
    </style>
    <div id="pageMSSQL" class="divPage">
        <div class="divTitleText"></div>
        <div class="divCondition">
            <div>
                <button id="btnConnect" class="jk-button mr10">连接</button>
                <button id="btnGenerat" class="jk-button-disabled mr10" disabled="disabled">生成</button>
                <button id="btnExport" class="jk-button-disabled mr10" disabled="disabled">下载</button>
                <button id="btnExportAll" class="jk-button-disabled" style="width:110px" disabled="disabled">下载全部表
                </button>
            </div>
            <table style="margin-top: 10px;width:100%">
                <tr>
                    <td width="90px">数据库类型：</td>
                    <td>
                        <!--<input type="radio" name="dbType" id="cboSqlServer" checked="checked" value="SqlServer"/>-->
                        <!--<label for="cboSqlServer">Sql Server</label>-->
                        <input type="radio" name="dbType" id="cboMySql" value="MySql"/>
                        <label for="cboMySql">MySql</label>
                        <input type="radio" name="dbType" id="cboPostgreSql" checked="checked" value="PostgreSql"/>
                        <label for="cboPostgreSql">PostgreSql</label>
                    </td>
                </tr>
                <tr>
                    <td>连接字符串：</td>
                    <td>
                        <input type="text" id="txtConnectionString" style="max-width: 686px;width: 100%"
                               value="jdbc:postgresql://xxxxxxxxxxxx:xxxx/xxx"/>
                    </td>
                </tr>
                <tr>
                    <td>用户名：</td>
                    <td>
                        <input type="text" id="txtUsername" style="max-width: 686px;width: 100%"
                               value="xxxxxxxx"/>
                    </td>
                </tr>
                <tr>
                    <td>密码：</td>
                    <td>
                        <input type="text" id="txtPassword" style="max-width: 686px;width: 100%"
                               value="xxxxxxxx"/>
                    </td>
                </tr>
                <tr class="eleHide">
                    <td>命名空间：</td>
                    <td>
                        <input type="text" id="txtPackage" style="max-width: 686px;width: 100%"
                               value="com.xxx.xxx.{0}"/>
                    </td>
                </tr>
                <tr class="eleHide">
                    <td>表和视图：</td>
                    <td>
                        <select id="sltTables" style="max-width:700px;width:100%"></select>
                    </td>
                </tr>
                <tr class="eleHide">
                    <td>类名：</td>
                    <td>
                        <input type="text" id="txtClassName" class="" style="max-width: 686px;width: 100%"/>
                    </td>
                </tr>
                <tr class="eleHide">
                    <td>类型：</td>
                    <td>
                        <select id="sltDataType" style="max-width:700px;width:100%"></select>
                    </td>
                </tr>
            </table>
        </div>
        <pre id="divCode" class="divContent" style="display: none"></pre>
        <form method="post" id="fmExport" style="display: none" action="/export/exportFile">
            <input name="content"/>
            <input name="fileName"/>
            <input name="extension" value="java"/>
        </form>
        <form method="post" id="fmExportAll" style="display: none" action="/export/exportAllFile">
            <input name="lang"/>
            <input name="className"/>
            <input name="connectionString"/>
            <input name="username"/>
            <input name="password"/>
            <input name="packagePath"/>
            <input name="dbType"/>
            <input name="type"/>
            <input name="extension" value="java"/>
        </form>
    </div>
    <script type="text/javascript">

        $(function () {
            if (localStorage.getItem("conn") != null) {
                $("#txtConnectionString").val(localStorage.getItem("conn"));
            }
            if (localStorage.getItem("username") != null) {
                $("#txtUsername").val(localStorage.getItem("username"));
            }
            if (localStorage.getItem("pwd") != null) {
                $("#txtPassword").val(localStorage.getItem("pwd"));
            }
            if (localStorage.getItem("dbtype") != null) {
                $("input[value=" + localStorage.getItem("dbtype") + "]").prop("checked", "checked");
            }
            InitCodeTypeSlt();
        });

        function InitCodeTypeSlt() {
            $.getJSON("/codeMaker/getCodeTypeSlt?lang=Java&r=" + new Date().getTime(), function (data) {
                $("#sltDataType").html('');
                let html = '';
                for (let i = 0; i < data.data.length; i++) {
                    html += "<option value='" + data.data[i] + "'>" + data.data[i].replaceAll("ref", "") + "</option>";
                }
                $("#sltDataType").html(html);
            });

        }

        $("#btnExport").bind("click", function () {
            const content = $("#divCode").text();
            if (content.trim() == '') return;
            $("#fmExport").find("input[name=content]").val(encodeURIComponent(content));
            $("#fmExport").find("input[name=fileName]").val($("#txtClassName").val());
            $("#fmExport").submit();
        });

        $("#btnExportAll").bind("click", function () {
            $("#fmExportAll").find("input[name=connectionString]").val(encodeURIComponent($("#txtConnectionString").val()));
            $("#fmExportAll").find("input[name=username]").val(encodeURIComponent($("#txtUsername").val()));
            $("#fmExportAll").find("input[name=password]").val(encodeURIComponent($("#txtPassword").val()));
            $("#fmExportAll").find("input[name=packagePath]").val($("#txtPackage").val());
            $("#fmExportAll").find("input[name=dbType]").val($("input:radio[checked=checked]").val());
            $("#fmExportAll").find("input[name=lang]").val("Java");
            $("#fmExportAll").find("input[name=type]").val($("#sltDataType").val());
            $("#fmExportAll").find("input[name=className]").val($("#txtClassName").val());
            $("#fmExportAll").submit();
        });

        document.body.oncopy = function () {
            setTimeout(function () {
                let text = clipboardData.getData("text");
                if (text) {
                    text = text + "\r\n本篇文章来源于 codetool 原文链接：" + location.href;
                    clipboardData.setData("text", text);
                }
            }, 100)
        }

        $("#btnGenerat").bind("click", function () {
            const connectionString = $("#txtConnectionString").val();
            const username = $("#txtUsername").val();
            const password = $("#txtPassword").val();
            const table = $("#sltTables").val();
            const pkg = $("#txtPackage").val();
            const className = $("#txtClassName").val();
            const type = $("#sltDataType").val();
            $.getJSON("/codeMaker/generatCode", {
                lang: "Java",
                dbType: $("input:radio[checked=checked]").val(),
                connectionString: encodeURIComponent(connectionString),
                username: encodeURIComponent(username),
                password: encodeURIComponent(password),
                table: table,
                packagePath: pkg,
                type: type,
                className: className,
                random: new Date().getTime()
            }, function (result) {
                if (result != null && result.code != '00000') {
                    alert(result.msg);
                }
                else {
                    $("#divCode").fadeIn(200);
                    $("#divCode").text(result.data);
                    $("#btnExport").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                    $("#btnExportAll").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                    if (type.indexOf('Xml') > 0) {
                        $("#fmExport,#fmExportAll").find("input[name=extension]").val('xml');
                    }
                    else {
                        $("#fmExport,#fmExportAll").find("input[name=extension]").val('java');
                    }
                }
            });
        });

        $("input:radio").bind("change", function () {
            const sqlserver = "jdbc:sqlserver://xxxxxxxxxxxx:xxxx;DatabaseName=xxx";
            const mysql = "jdbc:mysql://xxxxxxxxxxxx:xxxx/xxx";
            const postgresql = "jdbc:postgresql://xxxxxxxxxxxx:xxxx/xxx";

            if (this.value == 'SqlServer') {
                if ($("#txtConnectionString").val() == postgresql || $("#txtConnectionString").val() == mysql || $("#txtConnectionString").val() == "") {
                    $("#txtConnectionString").val(sqlserver);
                }
            } else if (this.value == 'PostgreSql') {
                if ($("#txtConnectionString").val() == sqlserver || $("#txtConnectionString").val() == mysql || $("#txtConnectionString").val() == "") {
                    $("#txtConnectionString").val(postgresql);
                }
            } else if (this.value == 'MySql') {
                if ($("#txtConnectionString").val() == sqlserver || $("#txtConnectionString").val() == postgresql || $("#txtConnectionString").val() == "") {
                    $("#txtConnectionString").val(mysql);
                }
            }
        });

        $("#divCode").bind("mouseover", function () {
            SelectText($(this));
        });

        function GetClassName(str) {
            if (str != null && str.length > 2 && str.substring(0, 2) == 't_') {
                str = str.substring(2);
            }
            return str.replaceUnderline().toUpperFirst();
        }

        $("#sltTables").bind("change", function () {
            $("#txtClassName").val(GetClassName($(this).find("option:selected").val()));
        });

        $("#btnConnect").bind("click", function () {
            const connectionString = $("#txtConnectionString").val().trim();
            if (connectionString.trim() == "") {
                return;
            }
            const username = $("#txtUsername").val().trim();
            if (username.trim() == "") {
                return;
            }
            const password = $("#txtPassword").val().trim();
            if (password.trim() == "") {
                return;
            }
            const dbType = $("input:radio[checked=checked]").val();
            localStorage.setItem("conn", connectionString);
            localStorage.setItem("username", username);
            localStorage.setItem("pwd", password);
            localStorage.setItem("dbtype", dbType);
            $.ajax({
                async: true,
                type: "POST",
                url: "/codeMaker/getTables",
                cache: false,
                timeout: 60 * 60 * 1000,
                dataType: "json",
                data: {
                    connectionString: encodeURIComponent(connectionString),
                    username: encodeURIComponent(username),
                    password: encodeURIComponent(password),
                    random: new Date(),
                    dbType: dbType
                },
                success: function (result) {
                    if (result != null && result.code != '00000') {
                        alert(result.msg);
                        $(".eleHide").hide();
                    }
                    else {
                        $(".eleHide").fadeIn(200);
                        $("#btnGenerat").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                        $("#btnExportAll").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                        let html = "";
                        if (result.data.length > 0) {
                            for (let i = 0; i < result.data.length; i++) {
                                html = html + "<option value='" + result.data[i]["TABLE_NAME"] + "'>" + result.data[i]["TABLE_NAME"] + "</option>";
                            }
                            $("#txtClassName").val(GetClassName(result.data[0]["TABLE_NAME"]));
                        }
                        $("#sltTables").html(html);
                    }
                }
            });
        });
    </script>
</@page.main>